<template>
  <dialog-editor ref="dialog"></dialog-editor>
</template>

<script>
import DialogEditor from '@/components/DialogEditor'
import dialogMixins from '@/utils/commonDialogMixins'

export default {
  mixins: [dialogMixins],
  components: {DialogEditor},
  data() {
    return {
      dataFormConfig: {
        id: {
          hidden: true
        },
        supplierListId: {
          label: '供应商',
          type: 'list',
          selectMultiple: true,
          placeholder: '请选择供应商',
          list: []
        },
        ingredientCategoryId: {
          label: '类别',
          type: 'list',
          placeholder: '请选择类别',
          list: []
        },
        ingredientName: {
          label: '原料名称',
          placeholder: '请输入原料名称'
        },
        warningNumber: {
          label: '预警值',
          type: 'number',
          max: 10000,
          placeholder: '请输入预警值'
        },
        status: {
          label: '状态',
          type: 'switch',
          activeValue: 1,
          inactiveValue: 0,
          default: 1
        }
      }
    }
  },
  methods: {
    init(id, property, cb) {
      this.dataForm.id = id || 0
      this.initIngredientCategoryList()
      this.initSupplierList('supplierListId')

      this.$refs.dialog.initDialog({
        dataForm: this.dataForm,
        config: this.dataFormConfig,
        dataRule: this.getNeedDataRule('supplierListId', 'ingredientName', 'warningNumber', 'ingredientCategoryId', 'status'),
        initApi: 'ingredientApi/ingredientInfoInfo',
        saveApi: 'ingredientApi/saveIngredientInfo',
        updateApi: 'ingredientApi/updateIngredientInfo',
        objectName: 'ingredientInfo',
        submitCallback: cb
      })

      this.$nextTick(() => {
        if (!this.R.isNil(property)) {
          Object.keys(property).forEach((key) => {
            this.dataForm[key] = property[key]
          })
        }
      })
    }
  }
}
</script>
